package com.atguigui.leetcode;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * 1417.重新格式化字符串
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/8/11 8:48
 */
public class P1417ReformatTheString {
    public static void main(String[] args) {
        Solution solution = new P1417ReformatTheString().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String reformat(String s) {
            Queue<Integer> nums = new ArrayDeque();
            Queue<String> chars = new ArrayDeque();
            int n = s.length();
            for (int i = 0; i < n; i++) {
                if (Character.isDigit(s.charAt(i))) {
                    nums.offer(Integer.valueOf(s.substring(i, i + 1)));
                } else {
                    chars.offer(s.substring(i, i + 1));
                }
            }

            int s1 = nums.size();
            int s2 = chars.size();


            if (Math.abs(s1 - s2) > 1) {
                return "";
            }

            StringBuilder sb = new StringBuilder();
            if (s1 > s2) {
                while (!chars.isEmpty()) {
                    sb.append(nums.poll());
                    sb.append(chars.poll());
                }
                sb.append(nums.poll());
            } else if (s1 < s2) {
                while (!nums.isEmpty()) {
                    sb.append(chars.poll());
                    sb.append(nums.poll());
                }
                sb.append(chars.poll());
            } else {
                while (!chars.isEmpty()) {
                    sb.append(chars.poll());
                    sb.append(nums.poll());
                }
            }
            return sb.toString();
        }
    }
}
